package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Delete extends JInternalFrame {
	
	Connection conn;
	private JTextField serial;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField code;
	private JTextField ea;
	private JButton btnDel;
	private JButton btnSearch;
	private JLabel status;
	
	
	DatabaseTest main;
	


	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Delete frame = new Delete();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Delete(Connection c,DatabaseTest main) {
		super("삭제", true, true, true, false);
		setBounds(0, 400, 300, 350);
		
		this.conn = c;
		getContentPane().setLayout(null);
		getContentPane().add(getSerial());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnDel());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getStatus());
		
		this.main = main;
		

	}

	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(77, 23, 125, 21);
		}
		return serial;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("시리얼:");
			label.setBounds(17, 25, 48, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("코드:");
			label_1.setBounds(17, 53, 41, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("수량: ");
			label_2.setBounds(17, 80, 41, 15);
		}
		return label_2;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(77, 50, 125, 21);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(77, 77, 125, 21);
		}
		return ea;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("삭제");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String sql = "delete from ptbl where serial=?"; //delete * from  *표 뺀다...ㅠㅠ
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s); 
						pstmt.executeUpdate();  //executeUpdate()의 반환타입은 정수형.쿼리가 실행되어 반환된 행의수를 뜻함.. 
												//데이터가 없을때까지면 if(executeUpdate = 0) 분기처리하면됨.??
						
						pstmt.close();
						
						status.setText("데이터가 삭제 되었습니다.");
						ea.setText("");
						code.setText("");		
						serial.requestFocus();
						
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					
				}
			});
			btnDel.setBounds(105, 108, 97, 23);
		}
		return btnDel;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("검색");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent ev) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial=?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s); 
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));
						}else{
							status.setText("삭제할 데이터가 없습니다.");
						}

						
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					
					
				}
			});
			btnSearch.setBounds(209, 21, 63, 23);
		}
		return btnSearch;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요.");
			status.setHorizontalAlignment(SwingConstants.CENTER);
			status.setBounds(12, 264, 259, 15);
		}
		return status;
	}
}
